package com.app.ui.activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.app.R

import com.app.util.DBOpenHelper


class RegisteredActivity : AppCompatActivity(), View.OnClickListener {
    private var et_rgsName: EditText? = null
    private var et_rgsEmail: EditText? = null
    private var et_rgsPhoneNum: EditText? = null
    private var et_rgsPsw1: EditText? = null
    private var et_rgsPsw2: EditText? = null
    private var mDBOpenHelper: DBOpenHelper? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR //禁止横屏
        setContentView(R.layout.activity_registered)
        title = "用户注册" //顶部标题改成用户注册
        initView() //初始化界面
        mDBOpenHelper = DBOpenHelper(this)
    }

    private fun initView() {
        et_rgsName = findViewById(R.id.et_rgsName)
        et_rgsEmail = findViewById(R.id.et_rgsEmail)
        et_rgsPhoneNum = findViewById(R.id.et_rgsPhoneNum)
        et_rgsPsw1 = findViewById(R.id.et_rgsPsw1)
        et_rgsPsw2 = findViewById(R.id.et_rgsPsw2)
        val btn_register = findViewById<Button>(R.id.btn_rgs)
        val iv_back = findViewById<ImageView>(R.id.iv_back)
        iv_back.setOnClickListener(this)
        btn_register.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_back -> {
                val intent = Intent(this@RegisteredActivity, LoginActivity::class.java)
                startActivity(intent)
                finish()
            }
            R.id.btn_rgs -> {
                //获取用户输入的用户名、密码、验证码
                val username = et_rgsName!!.text.toString().trim { it <= ' ' }
                val password1 = et_rgsPsw1!!.text.toString().trim { it <= ' ' }
                val password2 = et_rgsPsw2!!.text.toString().trim { it <= ' ' }
                val email = et_rgsEmail!!.text.toString().trim { it <= ' ' }
                val phonenum = et_rgsPhoneNum!!.text.toString().trim { it <= ' ' }
                //注册验证
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2)
                    && password1.length>=6 && password2.length>=6 && phonenum.length==11
                ) {
                    //判断两次密码是否一致
                    if (password1 == password2) {
                        //将用户名和密码加入到数据库中
                        mDBOpenHelper!!.add(username, password2, email, phonenum)
                        val intent1 = Intent(this@RegisteredActivity, LoginActivity::class.java)
                        startActivity(intent1)
                        finish()
                        Toast.makeText(this, "验证通过，注册成功", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this, "两次密码不一致,注册失败", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Toast.makeText(this, "注册信息不符合要求,注册失败", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}
